package com.example.huixue.service;

import com.example.huixue.entity.Category;
import com.example.huixue.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryService {
    
    @Autowired
    private CategoryMapper categoryMapper;
    
    /**
     * 获取所有分类
     */
    public List<Category> getAllCategories() {
        return categoryMapper.selectAll();
    }
    
    /**
     * 获取顶级分类
     */
    public List<Category> getTopLevelCategories() {
        return categoryMapper.selectTopLevel();
    }
    
    /**
     * 根据父分类ID获取子分类
     */
    public List<Category> getCategoriesByParentId(Long parentId) {
        return categoryMapper.selectByParentId(parentId);
    }
    
    /**
     * 根据ID获取分类
     */
    public Category getCategoryById(Long categoryId) {
        return categoryMapper.selectById(categoryId);
    }
    
    /**
     * 创建分类
     */
    public Category createCategory(Category category) {
        categoryMapper.insert(category);
        return category;
    }
    
    /**
     * 更新分类
     */
    public Category updateCategory(Category category) {
        categoryMapper.update(category);
        return category;
    }
    
    /**
     * 删除分类
     */
    public boolean deleteCategory(Long categoryId) {
        return categoryMapper.delete(categoryId) > 0;
    }
} 